// 基础南丁格尔玫瑰图组件
import BasePieChartComponent from "./base-pie-chart.js";

class NightingaleChart extends BasePieChartComponent {
    constructor() {
        super();
    }

    // 重写默认配置，添加玫瑰图特有属性
    getDefaultConfig() {
        const baseConfig = super.getDefaultConfig();
        return {
            ...baseConfig,
            seriesAttrs: [
                ...baseConfig.seriesAttrs,
                ['series-radius_text', '[20, 100]'], // 保持字符串格式，便于配置存储
                ['series-rosetype_text', 'area'], // radius 或 area
                ['series-startangle_number', 0],
                ['series-minangle_number', 20],
            ]
        };
    }

    // 重写观察属性
    static get observedAttributes() {
        return [
            ...super.observedAttributes,
            'series-radius_text',
            'series-rosetype_text',
            'series-startangle_number',
            'series-minangle_number',
        ];
    }

    // 重写生成系列配置
    generateSeries(pieData, seriesConfig, pieConfig) {
        const baseSeries = super.generateSeries(pieData, seriesConfig, pieConfig);

        let radius = ['40%', '70%']; // 保底默认数组
        try {
            // 解析配置中的字符串为数组（如"[\"40%\", \"70%\"]" → ["40%", "70%"]）
            if (seriesConfig.radius) {
                radius = JSON.parse(seriesConfig.radius);
                // 校验解析结果是否为数组
                if (!Array.isArray(radius)) {
                    throw new Error('解析结果不是数组');
                }
            }
        } catch (e) {
            console.warn('radius配置解析失败，使用默认数组:', e);
        }

        return [{
            ...baseSeries[0],
            roseType: seriesConfig.rosetype,
            minAngle: seriesConfig.minAngle,
            startAngle: seriesConfig.startangle,
            clockWise: false, // 逆时针排列
            radius: radius,
        }];
    }

    // 处理玫瑰图特有属性变化
    handleAttributeChange(name, value, currentOptions) {
        super.handleAttributeChange(name, value, currentOptions);
        
        if (!currentOptions.series || currentOptions.series.length === 0) return;

        switch(name) {
            case 'nightingale-rose-type_select':
                currentOptions.series[0].roseType = value;
                this.chart.setOption(currentOptions);
                break;
            case 'nightingale-min-angle_number':
                currentOptions.series[0].minAngle = parseInt(value);
                this.chart.setOption(currentOptions);
                break;
            case 'nightingale-offset_angle_number':
                currentOptions.series[0].startAngle = 90 + parseInt(value);
                this.chart.setOption(currentOptions);
                break;
        }
    }
}

customElements.define('nightingale-chart', NightingaleChart);
    